package com.cnebula.uas.sso.service.impl;

import javax.xml.bind.JAXBException;

import org.springframework.stereotype.Service;

import com.cnebula.uas.sso.model.OKUser;
import com.cnebula.uas.sso.model.Organization;
import com.cnebula.uas.sso.model.Personal;
import com.cnebula.uas.sso.model.UMPrincipal;
import com.cnebula.uas.sso.model.User;
import com.cnebula.uas.sso.service.UMPrincipalService;
import com.cnebula.uas.sso.util.JaxbUtil;

@Service
public class UMPrincipalServiceImpl implements UMPrincipalService {

	@Override
	public UMPrincipal okUserToUM(OKUser okUser) throws Exception{
		
		User user = okUser.getUser();
		Personal personal = user.getPersonal();

		UMPrincipal um = new UMPrincipal();

		um.setLocalLoginId(user.getUsername());
		um.setName(user.getUsername());
		if(personal != null){
			um.setEmail(personal.getEmail());
			um.setPhone(personal.getPhone());
			um.setBirthday(personal.getDateOfBirth());
		}
		
		Organization org = new Organization();
		
		return um;
	}

	@Override
	public String exportXML(UMPrincipal um) throws JAXBException {
		String content = JaxbUtil.beanToXml(um);
		return content;
	}
	
	public static void main(String[] args){
		UMPrincipal um = new UMPrincipal();
		um.setLoginId("zhangsan");
		um.setName("zhangsan");
		um.setLocalLoginId("zhangsan");
		
		UMPrincipalServiceImpl umService = new UMPrincipalServiceImpl();
		
		try {
			System.out.println(umService.exportXML(um));
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}

}
